package com.pwc.LDAP;

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;

/**
 * PWC LDAP
 * 
 * @author whuang072
 *
 */
public class LDAP {

	private Hashtable<String, String> env = new Hashtable<String, String>();
	private DirContext ctx = null;
	private NamingEnumeration results = null;
	private String securityPrincipal = "CN={GUID},OU=PwC Accounts,OU=Accounts and Groups,OU=US,DC=nam,DC=ad,DC=pwcinternal,DC=com";

	private Attributes attributes = null;

	private boolean status = false;
	private String errorMessage = "";

	private String GUID;

	/**
	 * init
	 * 
	 * @param GUID
	 * @param pwd
	 */
	public void init(String GUID, String pwd) {
		this.GUID = GUID;
		env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
		env.put(Context.PROVIDER_URL, "ldap://matlkifs1adp103.nam.ad.pwcinternal.com:389");
		env.put(Context.SECURITY_AUTHENTICATION, "simple");
		env.put(Context.SECURITY_PRINCIPAL, securityPrincipal.replace("{GUID}", this.GUID));
		env.put(Context.SECURITY_CREDENTIALS, pwd);
	}

	public boolean callLDAP() {
		try {
			ctx = new InitialDirContext(env);
			SearchControls controls = new SearchControls();
			controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
			results = ctx.search(securityPrincipal.replace("{GUID}", this.GUID), "(objectclass=*)", controls);
			while (results.hasMore()) {
				SearchResult searchResult = (SearchResult) results.next();
				attributes = searchResult.getAttributes();
			}
			status = true;
		} catch (NamingException e) {
			errorMessage = "not found,GUID or password error!";
			status = false;
		} catch (Exception e) {
			errorMessage = e.getMessage();
			status = false;
		} finally {
			return status;
		}
	}

	/**
	 * get employee number
	 * 
	 * @return 0 error
	 */
	public long getEmployeenumber() {
		String result = getAttribute("employeenumber");

		if (result != null) {
			return Long.parseLong(result);
		} else {
			return 0;
		}
	}

	/**
	 * get display name
	 * 
	 * @return
	 */
	public String getDisplayName() {
		return getAttribute("displayname");
	}

	/**
	 * get result from key
	 * @param Key
	 * @return
	 */
	public String getAttribute(String Key) {
		if (status && attributes != null) {
			try {
				Attribute attr = attributes.get(Key);
				return (String) attr.get();
			} catch (NamingException e) {
				errorMessage = "not found in search attribute!";
				status = false;
				return null;
			}
		}

		return null;
	}

	/***
	 * 
	 * getter and setter
	 */

	public boolean isStatus() {
		return status;
	}

	public String getErrorMessage() {
		return errorMessage;
	}

	public String getGUID() {
		return GUID;
	}

	public Attributes getAttributes() {
		return attributes;
	}

}
